//package com.ita.util;
//
//import java.security.Key;
//import java.security.SecureRandom;
//
//import javax.crypto.Cipher;
//import javax.crypto.SecretKey;
//import javax.crypto.SecretKeyFactory;
//import javax.crypto.spec.DESKeySpec;
//
//public class DESUtil {
//
//	private static DESUtil instance = new DESUtil();
//
//	private DESUtil() {
//
//	}
//
//	public static DESUtil getInstance() {
//		return instance;
//	}
//
//	private int parse(char c) {
//		if (c >= 'a')
//			return (c - 'a' + 10) & 0x0f;
//		if (c >= 'A')
//			return (c - 'A' + 10) & 0x0f;
//		return (c - '0') & 0x0f;
//	}
//
//	private byte[] String2Bytes(String str) {
//		byte[] b = new byte[str.length()/2];
//		int j = 0;
////		int l = 0;
////		int k = b.length - 1;
//		for (int i = 0; i < b.length; i++) {
//			char c0 = str.charAt(j++);
//			char c1 = str.charAt(j++);
////			char c2 = str.charAt(l++);
//			b[i] = (byte) ((parse(c0) << 4) | parse(c1) );
//		}
//		return b;
//	}
//
//	private SecretKey CreateKey(String key) {
//		byte input[] = String2Bytes(key);
//		DESKeySpec des;
//		try {
//			des = new DESKeySpec(input);
//			SecretKeyFactory keyFactory = SecretKeyFactory
//					.getInstance(Constant.DES_ALGORITHM);
//			SecretKey securekey = keyFactory.generateSecret(des);
//			return securekey;
//		} catch (Exception e) {
//			e.printStackTrace();
////			System.out.println(e.getMessage());
//			return null;
//		}
//
//	}
//
//	public String encrypt(String data, String key) {
//		Key deskey = CreateKey(key);
//		Cipher cipher;
//		try {
//			cipher = Cipher.getInstance(Constant.CIPHER_ALGORITHM);
//			SecureRandom random = new SecureRandom();
//			cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
//			byte[] results = cipher.doFinal(data.getBytes());
//			return Base64Util.byteToBase64(results);
//		} catch (Exception e) {
//			e.printStackTrace();
////			System.out.println(e.getMessage());
//			return null;
//		}
//
//	}
//
//	public String decrypt(String data, String key) {
//		Key deskey = CreateKey(key);
//		Cipher cipher;
//		try {
//			cipher = Cipher.getInstance(Constant.CIPHER_ALGORITHM);
//			cipher.init(Cipher.DECRYPT_MODE, deskey);
//			byte[] tmp = Base64Util.base64ToByte(data);
//			return new String(cipher.doFinal(tmp));
//			
//		} catch (Exception e) {
////			System.out.println(e.getMessage());
//			e.printStackTrace();
//			return null;
//		}
//
//	}
//
//}

package com.ita.util;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESUtil {

	private static DESUtil instance = new DESUtil();

	private DESUtil() {

	}

	public static DESUtil getInstance() {
		return instance;
	}
	
	private int parse(char c) {
		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;
	}

	private byte[] String2Bytes(String str) {
		byte[] b = new byte[str.length() / 2];
		int j = 0;
		for (int i = 0; i < b.length; i++) {
			char c0 = str.charAt(j++);
			char c1 = str.charAt(j++);
			b[i] = (byte) ((parse(c0) << 4) | parse(c1));
		}
		return b;
	}

	private SecretKey keyGenerator(String keyStr) throws Exception {
		byte input[] = String2Bytes(keyStr);
		DESKeySpec desKey = new DESKeySpec(input);
		SecretKeyFactory keyFactory = SecretKeyFactory
				.getInstance(Constant.DES_ALGORITHM);
		SecretKey securekey = keyFactory.generateSecret(desKey);
		return securekey;
	}



	public String encrypt(String data, String key) throws Exception {
		Key deskey = keyGenerator(key);
		Cipher cipher = Cipher.getInstance(Constant.CIPHER_ALGORITHM);
		SecureRandom random = new SecureRandom();
		cipher.init(Cipher.ENCRYPT_MODE, deskey, random);
		byte[] results = cipher.doFinal(data.getBytes());
		return Base64Util.byteToBase64(results);
	}

	public String decrypt(String data, String key) throws Exception {
		Key deskey = keyGenerator(key);
		Cipher cipher = Cipher.getInstance(Constant.CIPHER_ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, deskey);
		return new String(cipher.doFinal(Base64Util.base64ToByte(data)));
	}

}
